/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * under the License.
 */
package shopping.management.onthefly;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseOnTheFlyAction;
import shopping.commons.Util;
import shopping.model.Group;

import java.util.List;

/**
 * Copyright 2010 mazhao.
 * <p/>
 * User: mazhao
 * Date: 2010/11/28
 * Time: 19:20:39
 */
@org.apache.struts2.convention.annotation.Namespace("/management/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value = "shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
        @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
        @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class GroupAction extends BaseOnTheFlyAction implements ModelDriven<Group> {

    /** Model Object */
    private Group group = new Group();
    
    /**
     * create new group
     * @return
     */
    @Action(value = "/management/onthefly/groupcreate",
            results = {
                    @Result(name = "success", type = "json", params = {"ignoreHierarchy", "false"}),
                    @Result(name = "input", type = "json", params = {"ignoreHierarchy", "false"})

            }
    )
    public String create() {
        log.debug("enter management/onthefly/groupcreate action");
        log.debug("create group for:" + group);

        this.getModel().setCreator(Util.Security.retriveUserFromSession().getUsername());
        try {
            this.getModel().save();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw ex;
        }
        return SUCCESS;
    }

    private List<Group> groups ;

    public List<Group> getGroups() {
        return groups;
    }

    private int productId;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    @Action(value = "/management/onthefly/grouplist",
            results = {
                    @Result(name = "success", type = "json", params = {"ignoreHierarchy", "false"})
            }
    )
    public String list() {
        if(log.isDebugEnabled()) {
            log.debug("enter management/onthefly/grouplist action");
        }
        this.groups = Group.getGroupsOf(this.getProductId());
        if(log.isDebugEnabled()) {
            log.debug("find group cnt:" + groups.size());
            for(Group g : groups) {
                log.debug(g);
            }
        }
        return SUCCESS;
    }

    // --------------------------
    // Model Method.
    // --------------------------
    @Override
    public Group getModel() {
        return group;
    }
}
